package com.lss233.recipeguide.listener;

import com.lss233.recipeguide.RecipeData;
import com.lss233.recipeguide.RecipeGuide;
import com.lss233.recipeguide.Storage;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lss233
 */
public class GuiAddListener implements Listener {
    /*
        = = = = = = = = =       = : Frame
        = * * * = = = = =       * : Recipe item
        = * * * = = = X =       X : Result item
        = * * * = = = = =       - : Operation item
        = = = = = = = = =
        = - = - = = = = =
 */
    private final byte[] frame = new byte[]{
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 1, 1, 1, 0, 0, 0, 0, 0,
            0, 1, 1, 1, 0, 0, 0, 4, 0,
            0, 1, 1, 1, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 2, 3, 0, 0, 0, 0, 0, 0,
    };
    private RecipeGuide recipeGuide;

    public GuiAddListener(RecipeGuide recipeGuide) {
        this.recipeGuide = recipeGuide;
    }

    public static Map<Player, Inventory> getOpeningMap() {
        return openingMap;
    }

    private static Map<Player, Inventory> openingMap = new HashMap<>();

    @EventHandler
    public void onGuiOpen(InventoryOpenEvent event) {
        if (event.getInventory().equals(openingMap.get(event.getPlayer()))) {
            if (!event.getPlayer().hasPermission("lss233.recipe.new")) {
                event.getPlayer().closeInventory();
                return;
            }
            Inventory inventory = openingMap.get(event.getPlayer());
            for (int i = 0; i < frame.length; i++) {
                byte aFrame = frame[i];
                ItemStack item = null;
                ItemMeta meta = null;
                switch (aFrame) {
                    case 0:
                        item = new ItemStack(Material.STAINED_GLASS_PANE);
                        meta = item.getItemMeta();
                        meta.setDisplayName(ChatColor.GRAY + "=X=");
                        break;
                    case 2:
                        item = new ItemStack(Material.STONE_BUTTON);
                        meta = item.getItemMeta();
                        meta.setDisplayName(ChatColor.GRAY + "=保存");
                        break;
                    case 3:
                        item = new ItemStack(Material.WOOD_BUTTON);
                        meta = item.getItemMeta();
                        meta.setDisplayName(ChatColor.GRAY + "=取消");
                        break;
                    default:
                        item = new ItemStack(Material.AIR);
                        meta = item.getItemMeta();
                        break;
                }
                item.setItemMeta(meta);
                //inventory.addItem(item);
                inventory.setItem(i, item);
            }
        }
    }
@EventHandler
public void onClose(InventoryCloseEvent event){
        if(openingMap.containsValue(event.getInventory()))
            openingMap.remove(event.getPlayer());
}
    @EventHandler(priority = EventPriority.LOWEST)
    public void onGuiClick(InventoryClickEvent event) {
        try {
            if(openingMap.get(event.getWhoClicked()) == null) return;
            if (event.getClickedInventory().equals(openingMap.get(event.getWhoClicked()))) {
                if(event.getCurrentItem() == null) return;
                if (event.getWhoClicked().hasPermission("lss233.recipe.new")) {
                    if (event.getCurrentItem().getItemMeta()!=null && event.getCurrentItem().getItemMeta().getDisplayName().startsWith(ChatColor.GRAY + "=")) {
                        event.setCancelled(true);
                        if (event.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.GRAY + "=保存")) {
                            saveReipeGuide(event.getClickedInventory());
                            event.getWhoClicked().closeInventory();
                            event.getWhoClicked().sendMessage("[Recipe]Closed!!!!");
                        } else if (event.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.GRAY + "=取消")) {
                            event.getWhoClicked().closeInventory();
                        }
                    }
                }
            }
        } catch (Exception e) {
            RecipeGuide.sendThrowable(event.getWhoClicked(), e);
        }
    }

    private void saveReipeGuide(Inventory clickedInventory) throws IOException {

        RecipeData rd = new RecipeData();
        List<ItemStack> recipeItems = new ArrayList<>(9);
        for (int i = 0; i < frame.length; i++) {
            if (frame[i] == 1)
                recipeItems.add(clickedInventory.getItem(i));
            else if (frame[i] == 4)
                rd.setResultItem(clickedInventory.getItem(i));
        }
        rd.setRecipeItems(recipeItems);
        Storage.saveRecipe(rd);
    }
}
